package com.yanqu.road.server.cmd.activity.suoyao;

import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.SuoYaoActivityProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.suoyao.SuoYaoActivity;
import com.yanqu.road.server.manager.suoyao.SuoYaoMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = CrossProtocol.C2_SUOYAO_ACTIVITY_DRAW_HIDE_BOX, desc = "锁妖塔抽隐藏盲盒")
public class CrossSuoYaoDrawHideBoxCmd extends CrossCommand {

    int activityId;
    long serverId;
    long userId;
    String cost;
    int drawIndex;

    private boolean canDrawHide = false;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SuoYaoActivityProto.CrossSuoYaoDrawHideBoxReqMsg reqMsg = SuoYaoActivityProto.CrossSuoYaoDrawHideBoxReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        cost = reqMsg.getCost();
        serverId = reqMsg.getServerId();
        userId = packet.getPlayerId();
        drawIndex = reqMsg.getDrawIndex();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        SuoYaoActivity activity = SuoYaoMgr.getSuoYaoActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_SUOYAO_ACTIVITY_NO_OPEN;
            return;
        }

        canDrawHide = activity.drawHideBox();
    }

    @Override
    public void sendData() {
        SuoYaoActivityProto.CrossSuoYaoDrawHideBoxRespMsg.Builder builder = SuoYaoActivityProto.CrossSuoYaoDrawHideBoxRespMsg.newBuilder();
        builder.setRet(ret);
        builder.setCost(cost);
        builder.setDrawIndex(drawIndex);
        if (ret == 0) {
            builder.setIsHide(canDrawHide);
        }
        MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(GameProtocol.S_SUOYAO_ACTIVITY_DRAW_HIDE_BOX_FROM_CROSS, builder));
    }
}
